package com.pctx.bibatracker.util;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.math.BigDecimal;
import java.math.BigInteger;

public class MathUtils {
    public static BigDecimal getBigDecimal(Object value ) {
        BigDecimal ret = null;
        if( value != null ) {
            if( value instanceof BigDecimal ) {
                ret = (BigDecimal) value;
            } else if( value instanceof String ) {
                ret = new BigDecimal( (String) value );
            } else if( value instanceof BigInteger) {
                ret = new BigDecimal( (BigInteger) value );
            } else if( value instanceof Number ) {
                ret = new BigDecimal( ((Number)value).doubleValue() );
            } else {
                throw new ClassCastException("Not possible to coerce ["+value+"] from class "+value.getClass()+" into a BigDecimal.");
            }
        }
        return ret;
    }
    public static String  getCoordinateTransform(String cord){
        Object x=null;
        Object y=null;
        JSONArray jsonArray = JSONArray.fromObject(cord);
        Object[] os = jsonArray .toArray();
        for(int i=0; i<os.length; i++) {
            JSONObject jsonObj = JSONObject.fromObject(os[i]);
            JSONArray jsonArray1 = JSONArray.fromObject(jsonObj.get("result"));
            Object[] os1 = jsonArray1 .toArray();
            for(int j=0; j<os1.length; j++) {
                JSONObject jsonObj1 = JSONObject.fromObject(os1[i]);
                x=jsonObj1.get("x");
                y=jsonObj1.get("y");
            }
        }
        return x+","+y;

    }
}
